package queue;

public class MyLinkedList {
    class Node{
        public int val;
        public Node next;
        public Node(int val){
            this.val = val;
        }
    }

    public Node head;
    public Node last;
    private int usedSize;

    public void offer(int val){
        Node node = new Node(val);
        if(head == null){
            head = node;
            last = node;
            //尾插法
        }else{
            last.next = node;
            last = node;
        }
        this.usedSize++;
    }

    public int poll(){
        if(isEmpty()){
            throw new RuntimeException("no element");
        }
        int ret = head.val;
        head = head.next;
        //处理一个节点的情况
        if(head == null){
            last = null;
        }
        this.usedSize--;
        return ret;
    }

    public int peek(){
        if(isEmpty()){
            throw new RuntimeException("no element");
        }
        return head.val;
    }

    public boolean isEmpty(){
        return usedSize==0;
    }

}
